package com.mexico.inloancash.http;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 作者：Alex
 * 时间：2017/7/23 15:29
 * 简述：
 */
public class IndiaUploadProgressInterceptor implements Interceptor {
    private IndiaOnUploadListener onUploadListener;

    public IndiaUploadProgressInterceptor setOnUploadListener(IndiaOnUploadListener onUploadListener) {
        this.onUploadListener = onUploadListener;
        return this;
    }


    @Override
    public Response intercept(Chain chain) throws IOException {
        Request originalRequest = chain.request();
        if (originalRequest.body() == null || onUploadListener == null) {
            return chain.proceed(originalRequest);
        }
        IndiaProgressRequestBody progressRequestBody = new IndiaProgressRequestBody().setOriginalRequestBody(originalRequest.body()).setOnUploadListener(onUploadListener);
        Request progressRequest = originalRequest.newBuilder().method(originalRequest.method(), progressRequestBody).build();
        return chain.proceed(progressRequest);
    }
}
